var t = require("../../../../@babel/runtime/helpers/interopRequireDefault"), e = t(require("../../../../@babel/runtime/regenerator")), n = require("../../../../@babel/runtime/helpers/asyncToGenerator"), a = t(require("../../../../utils/weapp.qrcode.esm.js")), o = getApp();

Component({
    properties: {
        isshow: {
            value: !1,
            type: Boolean
        },
        showcarrfootBol: {
            type: null
        },
        iPnoneBottomBol: {
            type: null
        },
        kid: {
            type: null
        }
    },
    data: {
        errorFlag: !1,
        errorMsg: "",
        poster: "",
        poster_id: 0,
        loading: 0,
        getFlag: !0,
        step: 1.5,
        is_id: ""
    },
    ready: function() {},
    observers: {
        isshow: function(t) {
            t && "" == this.data.poster && this.data.getFlag && (this.getPosterPic(), this.data.getFlag = !1), 
            this.setData({
                is_id: this.data.kid
            });
        }
    },
    methods: {
        loadImgError: function(t) {
            this.setData({
                errorFlag: !0,
                errorMsg: t.detail.errMsg
            });
        },
        clicktapshow: function() {
            this.setData({
                isshow: !1
            }), this.triggerEvent("myshow", this.data.isshow);
        },
        previewImage: function() {
            wx.showToast({
                title: "长按分享好友",
                icon: "none",
                duration: 1800
            }), wx.previewImage({
                urls: [ this.data.poster ]
            });
        },
        getPosterPic: function() {
            var t = this;
            return n(e.default.mark(function n() {
                var a, s;
                return e.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return e.next = 2, t.getMyCanvasMessage(640, 1008);

                      case 2:
                        a = e.sent, s = o.getNetAddresss("plugin.skin-check.api.skin-check.poster"), t.setData({
                            canvasWidth: a.width,
                            canvasHeight: a.height
                        }), o._getNetWork({
                            showToastIn: !1,
                            url: s,
                            data: {
                                report_id: t.data.is_id,
                                ingress: "weChatApplet"
                            },
                            success: function(e) {
                                var n = e.data;
                                1 == n.result && (t.data.poster_id = e.data.data.id, t.startDrawCanvas(a, n.data));
                            }
                        });

                      case 6:
                      case "end":
                        return e.stop();
                    }
                }, n);
            }))();
        },
        getPosterTp2: function() {
            var t = this, e = this, n = o.getNetAddresss("member.poster");
            o._getNetWork({
                url: n,
                success: function(n) {
                    var a = n.data;
                    1 == a.result ? "" == a.data.image_url || null == a.data.image_url ? (wx.showToast({
                        title: "请稍等" + a.data.image_url,
                        icon: "none",
                        duration: 2e3
                    }), setTimeout(function() {
                        t.getPosterTp2();
                    }, 500)) : "" != a.data.image_url && setTimeout(function() {
                        e.setData({
                            poster: a.data.image_url
                        });
                    }, 1e3) : wx.showToast({
                        title: a.msg,
                        icon: "none",
                        duration: 1500
                    });
                },
                fail: function(t) {
                    this.data.timer && clearInterval(this.data.timer), wx.hideLoading({}), console.log(t);
                }
            });
        },
        startDrawCanvas: function(t, a) {
            var o = this;
            return n(e.default.mark(function s() {
                var i, r, c, l, h, u, d, g, f, w, p, m, v;
                return e.default.wrap(function(s) {
                    for (;;) switch (s.prev = s.next) {
                      case 0:
                        return (i = wx.createCanvasContext("myCanvas", o)).setFillStyle("#fff"), i.fillRect(0, 0, t.width, t.height), 
                        s.next = 5, o.readDownPicInfo(a.background);

                      case 5:
                        (r = s.sent).height > r.width ? (c = t.height / t.contentScale / r.height, r.height = r.height * c, 
                        r.width = r.width * c) : (l = t.width / t.contentScale / r.width, r.width = r.width * l, 
                        r.height = r.height * l), o.drawCanvasImage(i, {
                            left: 0,
                            top: 0,
                            width: r.width / o.data.step,
                            height: r.height / o.data.step,
                            img: r.img,
                            contentScale: t.contentScale
                        }), o.setData({
                            loading: 10
                        }), h = a.style_data, u = 0;

                      case 11:
                        if (!(u < h.length)) {
                            s.next = 45;
                            break;
                        }
                        if ((d = h[u]).contentScale = t.contentScale, console.log(d), "nickname" != d.type) {
                            s.next = 20;
                            break;
                        }
                        d.contentScale = 2 * d.contentScale, o.drawCanvasText(i, d), s.next = 40;
                        break;

                      case 20:
                        if ("logo" != d.type && "avatar" != d.type) {
                            s.next = 30;
                            break;
                        }
                        return s.next = 23, o.readDownPicInfo(d.src);

                      case 23:
                        g = s.sent, d.img = g.img, d.contentScale = 2 * d.contentScale, console.log("111111111", d), 
                        o.drawCanvasImage(i, d), s.next = 40;
                        break;

                      case 30:
                        if ("qr" != d.type) {
                            s.next = 39;
                            break;
                        }
                        return s.next = 33, o.readDownPicInfo(d.src);

                      case 33:
                        f = s.sent, d.img = f.img, d.contentScale = 2 * d.contentScale, o.drawCanvasImage(i, d), 
                        s.next = 40;
                        break;

                      case 39:
                        "score" == d.type ? (d.contentScale = 2 * d.contentScale, w = o.data.step, p = d.left * d.contentScale * w + d.width / 2, 
                        m = d.top * d.contentScale * w + d.height / 2, o.drawArc(d.src, p, m, i, d)) : "result" == d.type && (console.log(d), 
                        d.contentScale = 2 * d.contentScale, o.drawEnd(i, d));

                      case 40:
                        v = parseInt(80 / h.length * (u + 1)), o.setData({
                            loading: v + 10
                        });

                      case 42:
                        u++, s.next = 11;
                        break;

                      case 45:
                        console.log("执行完成"), i.draw(!1, setTimeout(n(e.default.mark(function n() {
                            var a;
                            return e.default.wrap(function(e) {
                                for (;;) switch (e.prev = e.next) {
                                  case 0:
                                    return e.next = 2, o.toimage("myCanvas", t);

                                  case 2:
                                    a = e.sent, o.setData({
                                        loading: 100,
                                        poster: a
                                    }), o.upQrcodeImg(a);

                                  case 5:
                                  case "end":
                                    return e.stop();
                                }
                            }, n);
                        })), 1e3));

                      case 47:
                      case "end":
                        return s.stop();
                    }
                }, s);
            }))();
        },
        drawEnd: function(t, e) {
            var n = this.data.step, a = e.left * e.contentScale * n, o = e.top * e.contentScale * n, s = e.height / 3;
            this.roundRect(t, a, o, e.width, s, 10, e.color, e.src[0], e), this.roundRect(t, a, o + (s + 10), e.width, s, 10, e.color, e.src[1], e), 
            e.src[2] && this.roundRect(t, a, o + (s + 10 + s + 10), e.width, s, 10, e.color, e.src[2], e);
        },
        roundRect: function(t, e, n, a, o, s, i, r, c) {
            a < 2 * s && (s = a / 2), o < 2 * s && (s = o / 2), t.beginPath(), t.fillStyle = i, 
            t.arc(e + s, n + s, s, Math.PI, 1.5 * Math.PI), t.moveTo(e + s, n), t.lineTo(e + a - s, n), 
            t.lineTo(e + a, n + s), t.arc(e + a - s, n + s, s, 1.5 * Math.PI, 2 * Math.PI), 
            t.lineTo(e + a, n + o - s), t.lineTo(e + a - s, n + o), t.arc(e + a - s, n + o - s, s, 0, .5 * Math.PI), 
            t.lineTo(e + s, n + o), t.lineTo(e, n + o - s), t.arc(e + s, n + o - s, s, .5 * Math.PI, Math.PI), 
            t.lineTo(e, n + s), t.lineTo(e + s, n), t.fill(), t.closePath(), t.beginPath(), 
            t.setFontSize(20), t.setFillStyle(c.font_color), t.setTextBaseline("middle"), console.log(e, n), 
            t.fillText("".concat(r.target.target_name, ":").concat(r.value_desc ? r.value_desc : r.value), e + a / 2, n + o / 2), 
            t.restore();
        },
        drawArc: function(t, e, n, a, o) {
            var s = this.data.step, i = o.left * o.contentScale * s + o.width / 2, r = o.top * o.contentScale * s + o.height / 2;
            a.arc(i, r, o.width / 2, 0, 2 * Math.PI), a.setStrokeStyle("#f8f8f8"), a.setLineWidth("12"), 
            a.setLineCap("round"), a.stroke(), a.closePath(), a.restore(), a.save(), a.beginPath(), 
            a.setFontSize(40), a.setFillStyle("#ff5000"), a.setTextAlign("center"), a.setTextBaseline("middle"), 
            a.fillText(t, e - 10, n), a.restore(), a.beginPath(), a.setFontSize(24), a.setFillStyle("#ff5000"), 
            a.setTextAlign("center"), a.setTextBaseline("middle"), a.fillText("分", e + 24, n - 10), 
            a.restore();
            var c = 2 * Math.PI / 100 * t - .5 * Math.PI;
            a.arc(e, n, o.width / 2, -.5 * Math.PI, c), a.setStrokeStyle(o.color), a.setLineWidth("12"), 
            a.setLineCap("round"), a.stroke(), a.beginPath();
        },
        drawCanvasImage: function(t, e) {
            var n = this.data.step;
            console.log("左上角 x 坐标", e.left * e.contentScale * n), console.log("左上角 y 坐标", e.top * e.contentScale * n), 
            t.drawImage(e.img, e.left * e.contentScale * n, e.top * e.contentScale * n, e.width * e.contentScale * n, e.height * e.contentScale * n);
        },
        drawCanvasText: function(t, e) {
            var n = this.data.step;
            console.log(n), console.log(e);
            var a = e.font_size * e.contentScale, o = e.top * e.contentScale + a;
            console.log(a, o * n), t.setFontSize(a * n), t.setFillStyle(e.color), t.fillText(e.src, e.left * e.contentScale * n, e.top * e.contentScale * n + 2 * a), 
            console.log("完成");
        },
        toimage: function(t, e) {
            var n = this;
            return new Promise(function(a, o) {
                console.log(t, e), wx.canvasToTempFilePath({
                    x: 0,
                    y: 0,
                    width: e.width,
                    height: e.height,
                    destWidth: 4 * e.width,
                    destHeight: 4 * e.height,
                    canvasId: t,
                    success: function(t) {
                        var e = t.tempFilePath;
                        a(e);
                    },
                    fail: function(t) {
                        wx.showToast({
                            title: "图片导出失败" + t,
                            icon: "success",
                            duration: 2e3
                        });
                    }
                }, n);
            });
        },
        drawRoundRectPath: function(t, e, n, a) {
            t.beginPath(0), t.arc(e - a, n - a, a, 0, Math.PI / 2), t.lineTo(a, n), t.arc(a, n - a, a, Math.PI / 2, Math.PI), 
            t.lineTo(0, a), t.arc(a, a, a, Math.PI, 3 * Math.PI / 2), t.lineTo(e - a, 0), t.arc(e - a, a, a, 3 * Math.PI / 2, 2 * Math.PI), 
            t.lineTo(e, n - a), t.closePath(), t.clip();
        },
        readDownPicInfo: function(t) {
            var e = this;
            return console.log(t), new Promise(function(n, a) {
                wx.downloadFile({
                    url: t,
                    success: function(t) {
                        wx.getImageInfo({
                            src: t.tempFilePath,
                            success: function(e) {
                                n({
                                    img: t.tempFilePath,
                                    width: e.width,
                                    height: e.height
                                });
                            },
                            fail: function(t) {
                                e.data.getFlag = !0, console.log("图片读取出错", t.errMsg), wx.showToast({
                                    title: "图片读取出错" + t.errMsg,
                                    icon: "success",
                                    duration: 2e3
                                });
                            }
                        });
                    },
                    fail: function(t) {
                        e.data.getFlag = !0, console.log("图片下载出错", t), wx.showToast({
                            title: "图片下载出错" + t,
                            icon: "none",
                            duration: 2e3
                        });
                    }
                });
            });
        },
        getMyCanvasMessage: function(t, e) {
            var n = this;
            return new Promise(function(a, o) {
                wx.getSystemInfo({
                    success: function(o) {
                        var s = e / t, i = o.screenWidth - .2 * o.screenWidth, r = i * s, c = i / t;
                        i *= n.data.step, r *= n.data.step, a({
                            width: i,
                            height: r,
                            contentScale: c
                        });
                    },
                    fail: function(t) {
                        n.data.getFlag = !0;
                    }
                });
            });
        },
        getQRCodeUrl: function(t) {
            var o = this;
            return new Promise(function(s, i) {
                var r = o.data.step;
                o.setData({
                    tempQRCodeCanvasWidth: t.width * t.contentScale * r,
                    tempQRCodeCanvasHeight: t.height * t.contentScale * r
                }, n(e.default.mark(function i() {
                    return e.default.wrap(function(i) {
                        for (;;) switch (i.prev = i.next) {
                          case 0:
                            (0, a.default)({
                                width: (t.width * t.contentScale - 10) * r,
                                height: (t.height * t.contentScale - 10) * r,
                                canvasId: "tempQRCode",
                                background: "#fff",
                                _this: o,
                                x: 5 * r,
                                y: 5 * r,
                                text: t.src,
                                callback: function() {
                                    setTimeout(n(e.default.mark(function n() {
                                        var a;
                                        return e.default.wrap(function(e) {
                                            for (;;) switch (e.prev = e.next) {
                                              case 0:
                                                return e.next = 2, o.toimage("tempQRCode", {
                                                    width: t.width * t.contentScale * r,
                                                    height: t.height * t.contentScale * r
                                                });

                                              case 2:
                                                a = e.sent, s(a);

                                              case 4:
                                              case "end":
                                                return e.stop();
                                            }
                                        }, n);
                                    })), 500);
                                }
                            });

                          case 1:
                          case "end":
                            return i.stop();
                        }
                    }, i);
                })));
            });
        },
        pxToNum: function(t) {
            var e = t.substring(0, t.length - 2);
            return parseInt(e);
        },
        upQrcodeImg: function(t) {
            var e = o.getNetAddresss("upload.uploadPic");
            console.log(t), wx.uploadFile({
                url: e,
                filePath: t,
                name: "file",
                header: {
                    "Content-Type": "multipart/form-data"
                },
                formData: {
                    _wx_token: wx.getStorageSync("wx_token"),
                    ingress: 0
                },
                success: function(t) {
                    var e = JSON.parse(t.data);
                    console.log("上传成功", e), 1 == e.result && console.log(e.data.img_url);
                },
                fail: function(t) {
                    console.log("上传失败一一一", t);
                }
            });
        },
        addPosterRecord: function(t) {
            var e = o.getNetAddresss("member.qrcode.poster-record");
            console.log("开始写记录"), o._getNetWork({
                showToastIn: !1,
                url: e,
                data: {
                    image: t,
                    poster_id: this.data.poster_id
                },
                success: function(t) {}
            });
        },
        checkWritePotosAlbum: function() {
            var t = this;
            return n(e.default.mark(function n() {
                var a;
                return e.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return a = t, e.next = 3, t.getAuthSetting("scope.writePhotosAlbum");

                      case 3:
                        if (e.sent) {
                            e.next = 7;
                            break;
                        }
                        return wx.showModal({
                            title: "请开启授权",
                            content: "是否跳转设置页开启授权",
                            success: function(t) {
                                t.confirm ? wx.openSetting({
                                    success: function(t) {
                                        t.authSetting["scope.writePhotosAlbum"] ? a.saveImg() : console.log("还是没授权");
                                    }
                                }) : t.cancel && console.log("用户点击取消");
                            }
                        }), e.abrupt("return");

                      case 7:
                        t.saveImg();

                      case 8:
                      case "end":
                        return e.stop();
                    }
                }, n);
            }))();
        },
        saveImg: function() {
            console.log("执行");
            var t = this.data.poster;
            wx.downloadFile({
                url: t,
                success: function(t) {
                    wx.saveImageToPhotosAlbum({
                        filePath: t.tempFilePath,
                        success: function(t) {
                            wx.showToast({
                                title: "保存成功",
                                icon: "none",
                                duration: 2e3
                            });
                        },
                        fail: function(t) {
                            wx.showToast({
                                title: t.msg,
                                icon: "none",
                                duration: 1e3
                            }), console.log(t);
                        }
                    });
                }
            });
        },
        getAuthSetting: function(t) {
            return new Promise(function(e, n) {
                wx.getSetting({
                    success: function(n) {
                        n.authSetting[t] ? e(!0) : wx.authorize({
                            scope: t,
                            success: function() {
                                e(!0);
                            },
                            fail: function() {
                                e(!1);
                            }
                        });
                    },
                    fail: function() {
                        e(!1);
                    }
                });
            });
        }
    }
});